﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Coralcode.MvcBase.Client.Controllers;
using CoralNode.Core.Entity.Domains.User;
using CoralNode.Core.IService.User;
using CoralNode.MvcClient.Contexts;
using Gongfu.Helper;

namespace Gongfu.Controllers
{
    public class AccountController : MvcControllerBase
    {
        private readonly IUserService _userService;

        public AccountController(IUserService userService)
        {
            _userService = userService;
        }
        public string Code()
        {
            Random rd = new Random();
            int num = rd.Next(100000, 1000000);
            return num.ToString();
        }
        [HttpPost]
        public ActionResult GetCode(string phone)
        {
            string code = Code();
            CookieHelper.SetCookie("code", code, DateTime.Now.AddDays(365));
            string url = "http://api.yhouse.com/201512pandasmscode/" + phone + "/" + code;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            request.Method = WebRequestMethods.Http.Get;
            request.ContentType = "text/html";
            request.MediaType = "text/html";
            request.UserAgent = "IIS-DookayHttpClient/4.1.1 (Asp.Net)";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            
            return Content(code);
        }
        [HttpPost]
        public ActionResult Authorization(string phone,string code)
        {
            string currentCode = CookieHelper.GetCookieValue("code");
            if (!string.IsNullOrWhiteSpace(currentCode))
            {
                if (string.Equals(code, currentCode))
                {
                    UserDomain user = new UserDomain()
                    {
                        Account = phone,
                        Password = phone,
                        CreateTime = DateTime.Now
                    };
                    if (!_userService.IsConflictUser(user))
                    {
                        user.Create();
                        CoralNodeContext.Current.SetReceptionCurrentUser(user,365);
                    }
                    else
                    {
                        UserDomain currentUser = _userService.FindByAccount(phone);
                        CoralNodeContext.Current.SetReceptionCurrentUser(currentUser,365);
                    }
                    return Content("1");
                }
                else
                {
                    return Content("验证失败！");
                }
                
            }
            return Content("Error");
        }
        [HttpPost]
        public string ChangeCodeStatus()
        {
            UserDomain user = _userService.FindById(CoralNodeContext.Current.ReceptionUser.Id);
            if (string.IsNullOrWhiteSpace(user.Interest))
            {
                user.Interest = "1";
                user.Update();
                return "1";
            }
            else
            {
                return "0";
            }
        }
        [HttpPost]
        public string ChangeVoteStatus()
        {
            UserDomain user = _userService.FindById(CoralNodeContext.Current.ReceptionUser.Id);
            if (string.IsNullOrWhiteSpace(user.Company))
            {
                user.Company = "1";
                user.Update();
                return "1";
            }
            else
            {
                return "0";
            }
        }
        [HttpPost]
        public string SendYhouseCode()
        {
            UserDomain user = _userService.FindById(CoralNodeContext.Current.ReceptionUser.Id);
            if (string.IsNullOrWhiteSpace(user.Company))
            {
                string url = "http://api.yhouse.com/m/promocode/thirdParty/collect?promocode=YHOUSEKFP&mobilePhone=" +
                             user.Account;
                HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
                request.Method = WebRequestMethods.Http.Post;
                return "1";
            }
            else
            {
                return "0";
            }
        }
    }
}